home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0019_PRIMES3.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  5KB  |  180 lines

  1. {
  2.   Hi, to All:
  3.  
  4.    ...While recently "tuning up" one of my Programs I'm currently
  5.    working on, I ran a little test to Compare the perfomance
  6.    of the different versions of Turbo Pascal from 5.0 through
  7.    to 7.0. The results were quite suprizing, and I thought I'd
  8.    share this With you guys/gals.
  9.  
  10.    Here are the results of a "sieve" Program to find all the primes
  11.    in 1 - 100,000, running on my AMI 386SX-25 CPU desktop PC:
  12.  
  13.       CompILER    EXECUTION TIME    RELATIVE TIME FACtoR
  14.       ==================================================
  15.        TP 7.0        46.7 sec              1.00
  16.        TP 6.0       137.8 sec              2.95
  17.        TP 5.5       137.5 sec              2.94
  18.        TP 5.0       137.6 sec              2.95
  19.  
  20.    Running the same Program to find all the primes in 1 - 10,000,
  21.    running on my 8086 - 9.54 Mhz NEC V20 CPU laptop PC:
  22.  
  23.       CompILER    EXECUTION TIME    RELATIVE TIME FACtoR
  24.       ==================================================
  25.        TP 7.0        14.1 sec              1.00
  26.        TP 6.0        28.3 sec              2.00
  27.  
  28.   notE: This would seem to indicate that the TP 7.0 386 math-
  29.         library is kicking in when run on a 386 CPU.
  30.  
  31.   Here is the source-code to my "seive" Program:
  32. ------------------------------------------------------------------------
  33. }
  34.  {.$DEFinE DebugMode}
  35.  {$DEFinE SaveData}
  36.  
  37.  {$ifDEF DebugMode}
  38.    {$ifDEF VER70}
  39.      {$ifDEF DPMI}
  40.        {$A+,B-,D+,E-,F-,G-,I+,L+,N-,P+,Q+,R+,S+,T+,V+,X-}
  41.      {$else}
  42.        {$A+,B-,D+,E-,F-,G-,I+,L+,N-,O-,P+,Q+,R+,S+,T+,V+,X-}
  43.      {$endif}
  44.    {$else}
  45.      {$ifDEF VER60}
  46.        {$A+,B-,D+,E-,F-,G-,I+,L+,N-,O-,R+,S+,V+,X-}
  47.      {$else}
  48.        {$A+,B-,D+,E-,F-,I+,L+,N-,O-,R+,S+,V+}
  49.      {$endif}
  50.    {$endif}
  51.  {$else}
  52.    {$ifDEF VER70}
  53.      {$ifDEF DPMI}
  54.        {$A+,B-,D-,E-,F-,G-,I-,L-,N-,P-,Q-,R-,S+,T-,V-,X-}
  55.      {$else}
  56.        {$A+,B-,D-,E-,F-,G-,I-,L-,N-,O-,P-,Q-,R-,S+,T-,V-,X-}
  57.      {$endif}
  58.    {$else}
  59.      {$ifDEF VER60}
  60.        {$A+,B-,D-,E-,F-,G-,I-,L-,N-,O-,R-,S+,V-,X-}
  61.      {$else}
  62.        {$A+,B-,D-,E-,F-,I-,L-,N-,O-,R-,S+,V-}
  63.      {$endif}
  64.    {$endif}
  65.  {$endif}
  66.  
  67.               (* Find prime numbers - Guy McLoughlin, 1993.           *)
  68. Program Find_Primes;
  69.  
  70.   (***** Check if a number is prime.                                  *)
  71.   (*                                                                  *)
  72.   Function Prime({input } lo_in : LongInt) : {output} Boolean;
  73.   Var
  74.     lo_Stop,
  75.     lo_Loop : LongInt;
  76.   begin
  77.     if (lo_in mod 2 = 0) then
  78.       begin
  79.         Prime := (lo_in = 2);
  80.         Exit
  81.       end;
  82.     if (lo_in mod 3 = 0) then
  83.       begin
  84.         Prime := (lo_in = 3);
  85.         Exit
  86.       end;
  87.  
  88.     if (lo_in mod 5 = 0) then
  89.       begin
  90.         Prime := (lo_in = 5);
  91.         Exit
  92.       end;
  93.     lo_Stop := 7;
  94.     While ((lo_Stop * lo_Stop) <= lo_in) do
  95.       inc(lo_Stop, 2);
  96.     lo_Loop := 7;
  97.     While (lo_Loop < lo_Stop) do
  98.       begin
  99.         inc(lo_Loop, 2);
  100.         if (lo_in mod lo_Loop = 0) then
  101.           begin
  102.             Prime := False;
  103.             Exit
  104.           end
  105.       end;
  106.     Prime := True
  107.   end;        (* Prime.                                               *)
  108.  
  109.   (***** Check For File IO errors.                                    *)
  110.   (*                                                                  *)
  111.   Procedure CheckIOerror;
  112.   Var
  113.     by_Error : Byte;
  114.   begin
  115.     by_Error := ioresult;
  116.     if (by_Error <> 0) then
  117.       begin
  118.         Writeln('File Error = ', by_Error);
  119.         halt
  120.       end
  121.   end;        (* CheckIOerror.                                        *)
  122.  
  123. Var
  124.   bo_Temp       : Boolean;
  125.   wo_PrimeCount : Word;
  126.   lo_Temp,
  127.   lo_Loop       : LongInt;
  128.   fite_Data     : Text;
  129.  
  130. begin
  131.   lo_Temp := 100000;
  132.   {$ifDEF SaveData}
  133.     {$ifDEF VER50}
  134.       assign(fite_Data, 'PRIME.50');
  135.     {$endif}
  136.     {$ifDEF VER55}
  137.       assign(fite_Data, 'PRIME.55');
  138.     {$endif}
  139.     {$ifDEF VER60}
  140.       assign(fite_Data, 'PRIME.60');
  141.     {$endif}
  142.     {$ifDEF VER70}
  143.       assign(fite_Data, 'PRIME.70');
  144.     {$endif}
  145.     {$I-}
  146.     reWrite(fite_Data);
  147.     {$I+}
  148.     CheckIOerror;
  149.     {$endif}
  150.   wo_PrimeCount := 0;
  151.   For lo_Loop := 2 to lo_Temp do
  152.     if Prime(lo_Loop) then
  153.   {$ifDEF SaveData}
  154.       begin
  155.         Write(fite_Data, lo_Loop:6);
  156.         Write(fite_Data, ', ');
  157.         inc(wo_PrimeCount);
  158.         if ((wo_PrimeCount mod 10) = 0) then
  159.           Writeln(fite_Data)
  160.       end;
  161.     close(fite_Data);
  162.     CheckIOerror;
  163.   {$else}
  164.       inc(wo_PrimeCount);
  165.   {$endif}
  166.     Writeln(wo_PrimeCount, ' primes between: 1 - ', lo_Temp)
  167. end.
  168.  
  169. {
  170.    ...This little test would put TP 7.0's .EXE's between 2 to 3
  171.    times faster than TP4 - TP6 .EXE's. (I've found simmilar results
  172.    in testing other Programs I've written.) I guess this is one more
  173.    reason to upgrade to TP 7.0 .
  174.  
  175.    ...I'd be curious to see how StonyBrook's Pascal+ 6.1 Compares
  176.    to TP 7.0, in terms of execution speed With this Program.
  177.  
  178.                                - Guy
  179. }
  180.